package com.hdcx.Minfresh.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.CartWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.manager.CacheManager;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;

/**
 * Created by Administrator on 2016/11/18.
 */

public class CleanCacheActivity extends BaseActivity {
    TextView tv_size;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleancache_);
        initView();
        initData();
    }

    private void initView() {
        findViewById(R.id.tv_left).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_title)).setText("清除缓存");
        findViewById(R.id.layout_clean).setOnClickListener(this);
        tv_size = (TextView) findViewById(R.id.tv_size);
    }

    private void initData() {
        String totalCacheSize;
        totalCacheSize = "0kb";
        try {
            totalCacheSize = CacheManager.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tv_size.setText("当前缓存" + totalCacheSize);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.layout_clean:
                final android.app.AlertDialog.Builder alterDialog = new android.app.AlertDialog.Builder(this);
                alterDialog.setMessage("是否清空缓存？");
                alterDialog.setCancelable(true);
                alterDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserInfoBean loginWrapper = ShpfUtil.getObject(ShpfUtil.LOGIN);
                        CartWrapper cart = ShpfUtil.getObject(AppUtil.getUserId());
                        CacheManager.clearAllCache(CleanCacheActivity.this);
                        ShpfUtil.setObject(ShpfUtil.LOGIN, loginWrapper);
                        if (cart != null) {
                            ShpfUtil.setObject(AppUtil.getUserId(), cart);
                        }
                        tv_size.setText("当前缓存0kb ");
                        Toast.makeText(CleanCacheActivity.this, "成功清除缓存！", Toast.LENGTH_SHORT).show();
                    }
                });
                alterDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                alterDialog.show();
                break;
        }
    }

}
